<template>
  <el-dialog :title="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'" :close-on-click-modal="false" :visible.sync="visible" class="cu-dialog cu-dialog_center" lock-scroll width="800px">
    <el-row :gutter="15" class="">
      <el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :disabled="!!isDetail" :rules="rules">
        <el-col :span="12" v-if="custom.hasFormP('code')">
          <el-form-item label="编码" prop="code">
            <el-input v-model="dataForm.code" placeholder="请输入" clearable required :style="{ width: '100%' }"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="custom.hasFormP('name')">
          <el-form-item label="名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="请输入" clearable required :style="{ width: '100%' }"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="custom.hasFormP('points')">
          <el-form-item label="积分值" prop="points">
            <el-input-number v-model="dataForm.points" placeholder="请输入" clearable required :style="{ width: '100%' }"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="custom.hasFormP('sort')">
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="dataForm.sort" placeholder="请输入" clearable required :style="{ width: '100%' }"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="custom.hasFormP('describe')">
          <el-form-item label="描述" prop="describe">
            <el-input v-model="dataForm.describe" placeholder="请输入" show-word-limit :style="{ width: '100%' }" type="textarea" :autosize="{ minRows: 4, maxRows: 4 }"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import request from '@/utils/request'
export default {
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      dataForm: {
        id: '',
        name: undefined,
        code: undefined,
        type: 'Artificial',
        points: undefined,
        describe: undefined,
        sort: 0,
        status: true
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '请输入编码',
            trigger: 'blur'
          }
        ],
        points: [
          {
            required: true,
            message: '请输入积分值',
            trigger: 'blur'
          }
        ]
      },
      typeOptions: [{ fullName: '人工操作', id: 'Artificial' }, { fullName: '系统操作', id: 'System' }]
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    goBack() {
      this.$emit('refresh')
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0
      this.visible = true
      this.isDetail = isDetail || false
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          request({
            url: `/api/campus/integral/rule/info/${this.dataForm.id}`,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data
          })
        }
      })
    },
    dataFormSubmit() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (!this.dataForm.id) {
            request({
              url: `/api/campus/integral/rule`,
              method: 'post',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  ;(this.visible = false), this.$emit('refresh', true)
                }
              })
            })
          } else {
            request({
              url: `/api/campus/integral/rule/update/${this.dataForm.id}`,
              method: 'PUT',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>
